'''此模块用来封装接口访问方法'''

#以下代码用于调试数据
# import requests
# from config.Config import *
# session = requests.Session()

class Api:
    #用单例模式方法封装一个session对象
    @classmethod
    def set_session(cls,session):
        cls.session = session
    @classmethod
    def get_session(cls):
        return cls.session
    #封装接口访问的方法
    @classmethod
    def send_api(cls,api_ele):
        # api_ele = ['post','url','data','headers']
        type = [['post','put','patch'],['get','delete','head','options']]
        if api_ele[0] in type[0]:
            cls.reponse = cls.session.request(method=api_ele[0],url=api_ele[1],
                            data=api_ele[2],headers=api_ele[3])
        elif api_ele[0] in type[1]:
            cls.reponse = cls.session.request(method=api_ele[0],url=api_ele[1],
                            params=api_ele[2],headers=api_ele[3])
        return cls.reponse

    #封装断言的方法
    def api_text_assert(self):
        self.value = self.reponse.text      #获取文本断言
        return self.value
    def api_json_assert(self):
        self.value = self.reponse.json()
        # assert self.value['msg'] == '登录成功！'
        # print('登录成功')
        return self.value

if __name__ == '__main__':
    a = Api()
    a.set_session(session=session)
    a.get_session()
    cms_login = ['post',cms_login_url,cms_login_data,cms_login_headers]
    a.send_api(api_ele=cms_login)
    a.api_json_assert()

